﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;

using SIMA.Entities;
using SIMA.Core.Data.Collection;
using SIMA.Core.Data.Access;
using SIMA.Controls;

namespace SIMA.Client.Auxiliars.MVVM
{
    public class AdminTablaMvvm:AccessClass<T_C_Tabla>
    {
        #region Fields

        SmartContainer<T_C_Tabla> tablasSmart;
        List<T_C_Tabla> tablas;

        #endregion

        #region Properties

        public List<T_C_Tabla> Tablas
        {
            get { return tablas; }
            set { tablas = value; }
        }

        #endregion

        #region Constructor

        public AdminTablaMvvm()
        {
            Init();
        }

        #endregion

        #region Private Methods

        private void Init()
        {

            //Inicializando el SmartContainer
            DataAccess.SetConnection(SIMA.Core.Enums.AccessType.FromString, CADCONNECTIONSTRING);
            tablasSmart = new SmartContainer<T_C_Tabla>();
            tablasSmart.Inserted += new EventHandler<SIMA.Core.EventArgs.InsertedEventArgs>(tablasSmart_Inserted);
            tablasSmart.Failed += new EventHandler<SIMA.Core.EventArgs.FailedEventArgs>(tablasSmart_Failed);
            tablasSmart.SelectAll();
            tablas = tablasSmart.ToList();
        }

        void tablasSmart_Failed(object sender, SIMA.Core.EventArgs.FailedEventArgs e)
        {
            SIMAMessageBox.Show(e.InstantFailed.ToLongTimeString(), e.ExceptionOcurred.Message, e.QueryString, SIMAMessageBoxButtons.OK, SIMAMessageBoxImage.Error);
        }

        void tablasSmart_Inserted(object sender, SIMA.Core.EventArgs.InsertedEventArgs e)
        {
            //MessageBox.Show("Se agregó nuevo registro", e.InstantInserted.ToLongTimeString(), MessageBoxButton.OK, MessageBoxImage.Information);
            SIMAMessageBox.Show(e.InstantInserted.ToLongTimeString(), "Registro satisfactorio.", e.QueryString, SIMAMessageBoxButtons.OK, SIMAMessageBoxImage.Information);
        }

        #endregion

        #region Public Methods

        public override void Register(T_C_Tabla item)
        {
            tablasSmart.Add(item);
        }

        public override void Delete(T_C_Tabla item)
        {
            tablasSmart.Remove(item);
        }

        public override void Update(T_C_Tabla itemToUpdate, T_C_Tabla itemUpdated)
        {
            tablasSmart.Update(itemToUpdate, itemUpdated);
        }

        public override void SelectAll()
        {
            tablasSmart.SelectAll();
            tablas = tablasSmart.ToList();
        }

        #endregion       
        
    }
}
